# -*- coding: utf-8 -*-
import os
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.core.files.storage import FileSystemStorage
from pyecharts.charts import Bar
from pyecharts import options as opts
from ..utils import DataProcessor


@login_required
def upload_file(request):
    if request.method == 'POST' and request.FILES.get('file'):
        uploaded_file = request.FILES['file']
        fs = FileSystemStorage(location=os.path.join(os.path.dirname(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), 'media'))
        filename = fs.save(uploaded_file.name, uploaded_file)
        return redirect('user_dashboard')
    return redirect('user_dashboard')


@login_required
def dashboard_redirect_view(request):
    from .. import views
    if views.is_admin(request.user):
        return redirect('admin_dashboard')
    else:
        return redirect('user_dashboard')


@login_required
def user_settings_view(request):
    return render(request, 'user/user_settings.html')
